package com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.Impl;


import com.mongodb.client.result.DeleteResult;
import com.mongodb.client.result.UpdateResult;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.Group_Type;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.MessageType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.S_CType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Enum.StatusType;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.MongoDBEntity.*;
import com.vq3.digitalizationsoftwareintegrates.Entity.Spot_Check.S_C_DatesEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.Permission_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.URole_TBEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.UserEntity;
import com.vq3.digitalizationsoftwareintegrates.Entity.User.User_RoleEntity;
import com.vq3.digitalizationsoftwareintegrates.MongoDB.Spot_Check.MongoDB_S_C_Point_ContentDao;
import com.vq3.digitalizationsoftwareintegrates.Utils.DateUtils.GetServerDate;
import com.vq3.digitalizationsoftwareintegrates.Utils.ImageUtil.OnOrDownLoadImage;
import org.springframework.context.annotation.Import;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Import({OnOrDownLoadImage.class})
@Component
public class MongoDB_S_C_Point_ContentDaoImpl implements MongoDB_S_C_Point_ContentDao{
    @Resource
    OnOrDownLoadImage onOrDownLoadImage;
    @Resource
    MongoTemplate mongoTemplate;


    private MongoDB_S_C_WebSocketSendMessageEntity setMessageDate() {
        MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = new MongoDB_S_C_WebSocketSendMessageEntity();
        mongoDB_s_c_webSocketSendMessageEntity.setYear(GetServerDate.getYear());
        mongoDB_s_c_webSocketSendMessageEntity.setMonth(GetServerDate.getMonth());
        mongoDB_s_c_webSocketSendMessageEntity.setDay(GetServerDate.getDate());
        mongoDB_s_c_webSocketSendMessageEntity.setHour(GetServerDate.getHour());
        mongoDB_s_c_webSocketSendMessageEntity.setMinute(GetServerDate.getMinute());
        return mongoDB_s_c_webSocketSendMessageEntity;

    }

    @Override
    public void saveTempImg(List<MongoDB_S_C_Point_ContentEntity> s_c_point_contentEntities_JSON) {
        for (MongoDB_S_C_Point_ContentEntity s_c_point_contentEntity : s_c_point_contentEntities_JSON) {
            MongoDB_S_C_Point_ContentEntity save = mongoTemplate.save(s_c_point_contentEntity);
            System.out.println(s_c_point_contentEntity);
        }

    }

    // TODO: 2023/1/4 重载方法改这个->.addCriteria(Criteria.where("Status_Type").is(StatusType.NotSubmitted))
    //  ->.addCriteria(Criteria.where("Status_Type").is(StatusType.Submitted))
    @Override
    public List<MongoDB_S_C_Point_ContentEntity> findTempImage(String pointName, UserEntity userEntity, S_CType sCType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("UserName").is(userEntity.getUname()))
            .addCriteria(Criteria.where("S_C_Type").is(sCType))
                .addCriteria(Criteria.where("Status_Type").is(StatusType.NotSubmitted))
            .addCriteria(Criteria.where("ItemName").is(pointName));

        return mongoTemplate.find(query, MongoDB_S_C_Point_ContentEntity.class);
    }
    @Override
    public List<MongoDB_S_C_Point_ContentEntity> findTempImage(String pointName, UserEntity userEntity, S_CType sCType, StatusType statusType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("UserName").is(userEntity.getUname()))
                .addCriteria(Criteria.where("S_C_Type").is(sCType))
                .addCriteria(Criteria.where("Status_Type").is(statusType))
                .addCriteria(Criteria.where("ItemName").is(pointName));

        return mongoTemplate.find(query, MongoDB_S_C_Point_ContentEntity.class);
    }


    public Object reviseItem(String pointName, UserEntity userEntity, S_CType sCType, StatusType statusType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("UserName").is(userEntity.getUname()))
                .addCriteria(Criteria.where("ItemName").is(pointName))
                .addCriteria(Criteria.where("S_C_Type").is(sCType))
                .addCriteria(Criteria.where("Status_Type").is(statusType));
        List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities = mongoTemplate.find(query, MongoDB_S_C_Point_ContentEntity.class);
        onOrDownLoadImage.deleteTempImage(mongoDB_s_c_point_contentEntities);
        return mongoTemplate.remove(query, MongoDB_S_C_Point_ContentEntity.class);
    }
    public List<MongoDB_S_C_Point_ContentEntity> findOne(String post_check_Point_Name) {
        Query query = new Query();
        query.addCriteria(Criteria.where("ItemName").is(post_check_Point_Name));
        return mongoTemplate.find(query, MongoDB_S_C_Point_ContentEntity.class);

    }

    public boolean check_ok_submit(String pointName, StatusType statusType, Group_Type group_type) {
        Query query = new Query();
        Query query1 = new Query();
        Update update = new Update();
        query.addCriteria(Criteria.where("ItemName").is(pointName))
                .addCriteria(Criteria.where("Status_Type").is(statusType));
        query1.addCriteria(Criteria.where("ItemName").is(pointName))
                .addCriteria(Criteria.where("Status_Type").is(StatusType.Submitted));
        update.set("Status_Type",StatusType.Submitted);
        UpdateResult updateResult = mongoTemplate.updateMulti(query, update, MongoDB_S_C_Point_ContentEntity.class);
        List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities = mongoTemplate.find(query1, MongoDB_S_C_Point_ContentEntity.class);
        mongoTemplate.remove(query1,MongoDB_S_C_Point_ContentEntity.class);
        for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : mongoDB_s_c_point_contentEntities) {
            mongoDB_s_c_point_contentEntity.setGroup_type(group_type);
            mongoTemplate.save(mongoDB_s_c_point_contentEntity);
        }

        return updateResult.getMatchedCount() > 0 || updateResult.getModifiedCount() > 0;

    }
    public boolean saveWebSocketMessage(MongoDB_S_C_WebSocketMessageEntity messageEntity){
        MongoDB_S_C_WebSocketMessageEntity save;
        try {
            save = mongoTemplate.save(messageEntity);
        }catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        if (save.getPermission_tbEntities() == null) {
            return false;
        }
        return true;
    }
    public List<MongoDB_S_C_WebSocketMessageEntity> findMongoDB_S_C_WebSocketMessageEntity(List<URole_TBEntity> uRole_tbEntityS) {
        Query query = new Query();
        List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketMessageEntities;
        Criteria criteria = new Criteria();
        List<Criteria> criteriaList = new ArrayList<>();
        for (URole_TBEntity uRole_tbEntity : uRole_tbEntityS) {

            criteriaList.add(Criteria.where("uRole_tbEntity").is(uRole_tbEntity));
        }

        try {

            criteria.orOperator(criteriaList.toArray(new Criteria[0]));
            query.addCriteria(criteria);
        mongoDB_s_c_webSocketMessageEntities = mongoTemplate.find(query, MongoDB_S_C_WebSocketMessageEntity.class);
        }catch (NullPointerException e) {
            e.printStackTrace();
            return null;
        }
        return mongoDB_s_c_webSocketMessageEntities;
    }

    @Override
    public List<MongoDB_S_C_WebSocketMessageEntity> findMongoDB_S_C_WebSocketMessageEntity(Permission_TBEntity permission_tbEntity) {
        Query query = new Query();
        List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketMessageEntities = null;

        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_tbEntity.getPermission()).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.Submitted.toString())));
        System.out.println(query);
        mongoDB_s_c_webSocketMessageEntities = mongoTemplate.find(query, MongoDB_S_C_WebSocketMessageEntity.class);
        return mongoDB_s_c_webSocketMessageEntities;
    }



    public List<MongoDB_S_C_WebSocketMessageEntity> findMongoDB_S_C_WebSocketMessage(Permission_TBEntity permission_tbEntity) {
        Query query = new Query();
        query.addCriteria(Criteria.where("permission_tbEntities.permission").is(permission_tbEntity.getPermission()).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.Submitted.toString())));

        return mongoTemplate.find(query,MongoDB_S_C_WebSocketMessageEntity.class);
    }
    /**三种数据库类型，四个数据库，多张表联查
     * */
    public List<MongoDB_S_C_Point_ContentEntity> itemNameFindMongoDBToMongoDB_S_C_Point_ContentEntityS(List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketMessageEntities) {
        Query query = new Query();
        Criteria criteria = new Criteria();
        List<Criteria> criteriaList = new ArrayList<>();
        for (MongoDB_S_C_WebSocketMessageEntity mongoDB_s_c_webSocketMessageEntity : mongoDB_s_c_webSocketMessageEntities) {
            criteriaList.add(Criteria.where("ItemName").is(mongoDB_s_c_webSocketMessageEntity.getItemName()).andOperator(Criteria.where("Status_Type").is(StatusType.Submitted)));
        }
        criteria.orOperator(criteriaList.toArray(new Criteria[0]));
        query.addCriteria(criteria);
        return mongoTemplate.find(query,MongoDB_S_C_Point_ContentEntity.class);
    }

    public List<MongoDB_S_C_Point_ContentEntity> itemNameFindMongoDB_S_C_Point_ContentEntity(String itemName) {
        Query query = new Query();
        query.addCriteria(Criteria.where("ItemName").is(itemName).andOperator(Criteria.where("Status_Type").is(StatusType.Submitted)));
        return mongoTemplate.find(query,MongoDB_S_C_Point_ContentEntity.class);
    }
@Transactional(rollbackFor = Exception.class)
    public boolean S_C_AuditReviewNG(String itemName, StatusType statusType) {
        Query query1 = new Query();
        Update update1 = new Update();
        Query query = new Query();
        Update update = new Update();
        Query query2 = new Query();
        query2.addCriteria(Criteria.where("itemName").is(itemName).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.Submitted)));
        List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketSendMessageEntities = mongoTemplate.find(query2, MongoDB_S_C_WebSocketMessageEntity.class);

        String uname = mongoDB_s_c_webSocketSendMessageEntities.get(0).getLastUserEntity().getUname();
        if (uname != null){
            MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = setMessageDate();

            mongoDB_s_c_webSocketSendMessageEntity.setMessage('"'+ itemName+'"'+"审核不通过，请检查！");
            mongoDB_s_c_webSocketSendMessageEntity.setItemName(itemName);
            mongoDB_s_c_webSocketSendMessageEntity.setLastUname(uname);
            mongoDB_s_c_webSocketSendMessageEntity.setMessageType(MessageType.S_CAddAuditMessage);
            query1.addCriteria(Criteria.where("itemName").is(itemName).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.Submitted)));
            query.addCriteria(Criteria.where("ItemName").is(itemName).andOperator(Criteria.where("Status_Type").is(StatusType.Submitted)));
            update.set("Status_Type",statusType);
            update1.set("mongoDB_s_c_point_contentEntityS.$[].Status_Type",statusType);



            UpdateResult updateResult1 = mongoTemplate.updateMulti(query, update, MongoDB_S_C_Point_ContentEntity.class);
            if (updateResult1.getMatchedCount() > 0 || updateResult1.getModifiedCount() > 0) {
                UpdateResult updateResult = mongoTemplate.updateMulti(query1, update1, MongoDB_S_C_WebSocketMessageEntity.class);

                MongoDB_S_C_WebSocketSendMessageEntity save = mongoTemplate.save(mongoDB_s_c_webSocketSendMessageEntity);

                return (updateResult.getMatchedCount() > 0 || updateResult.getModifiedCount() > 0) || (updateResult1.getMatchedCount() > 0 || updateResult1.getModifiedCount() > 0) || save.getLastUname() != null;
            }else {
                return false;
            }
        }else {
            return false;
        }




    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean S_C_AuditReviewNG(String itemName, StatusType statusType, List<String> cleaningRemarkContext) {
        Query query1 = new Query();
        Update update1 = new Update();
        Query query = new Query();
        Update update = new Update();
        Query query2 = new Query();
        query2.addCriteria(Criteria.where("itemName").is(itemName).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.Submitted)));
        List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketSendMessageEntities = mongoTemplate.find(query2, MongoDB_S_C_WebSocketMessageEntity.class);
        String uname = mongoDB_s_c_webSocketSendMessageEntities.get(0).getLastUserEntity().getUname();
        if (uname != null) {
            MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = setMessageDate();
            mongoDB_s_c_webSocketSendMessageEntity.setMessage('"' + itemName + '"' + "审核不通过，请检查！");

            mongoDB_s_c_webSocketSendMessageEntity.setItemName(itemName);
            mongoDB_s_c_webSocketSendMessageEntity.setLastUname(uname);
            mongoDB_s_c_webSocketSendMessageEntity.setMessageType(MessageType.S_CAddAuditMessage);
            query1.addCriteria(Criteria.where("itemName").is(itemName).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.Submitted)));
            query.addCriteria(Criteria.where("ItemName").is(itemName).andOperator(Criteria.where("Status_Type").is(StatusType.Submitted)));
            update.set("Status_Type", statusType);
            update1.set("mongoDB_s_c_point_contentEntityS.$[].Status_Type", statusType);

            MongoDB_S_C_WebSocketMessageEntity mongoDB_s_c_webSocketMessageEntities = mongoTemplate.findOne(query1, MongoDB_S_C_WebSocketMessageEntity.class);
            if (mongoDB_s_c_webSocketMessageEntities == null) {
                throw new NullPointerException();

            } else {
                List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities = mongoTemplate.find(query, MongoDB_S_C_Point_ContentEntity.class);
                for (int i = 0; i < mongoDB_s_c_point_contentEntities.size(); i++) {
                    Query query3 = new Query();
                    Update update2 = new Update();

                    query3.addCriteria(Criteria.where("ItemName").is(itemName).and("Status_Type").is(StatusType.Submitted).and("Point_Content_Id").is(i + 1));
                    update2.set("remarkContext", cleaningRemarkContext.get(i)).set("Status_Type", statusType);

                    UpdateResult updateResult1 = mongoTemplate.updateMulti(query3, update2, MongoDB_S_C_Point_ContentEntity.class);
                    if (!(updateResult1.getMatchedCount() > 0 || updateResult1.getModifiedCount() > 0)) {
                        return false;
                    }else {
                        Query query4 = new Query();
                        Update update3 = new Update();
                        query4.addCriteria(Criteria.where("ItemName").is(itemName));
                        List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities1 = mongoTemplate.find(query4, MongoDB_S_C_Point_ContentEntity.class);
                        update3.set("mongoDB_s_c_point_contentEntityS", mongoDB_s_c_point_contentEntities1);
                        UpdateResult updateResult = mongoTemplate.updateMulti(query4, update3, MongoDB_S_C_WebSocketMessageEntity.class);
                        if (!(updateResult.getMatchedCount() > 0 || updateResult.getModifiedCount() > 0)) {
                            return false;
                        }
                    }

                }

                MongoDB_S_C_WebSocketSendMessageEntity save = mongoTemplate.save(mongoDB_s_c_webSocketSendMessageEntity);
                return save.getLastUname() != null;
            }
        }else {
            return false;
        }

    }

    public String itemNameFindUName(String itemName,StatusType statusType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("itemName").is(itemName).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(statusType)));
        List<MongoDB_S_C_WebSocketMessageEntity> mongoDB_s_c_webSocketSendMessageEntities = mongoTemplate.find(query, MongoDB_S_C_WebSocketMessageEntity.class);
        try {
            return mongoDB_s_c_webSocketSendMessageEntities.get(0).getLastUserEntity().getUname();
        }catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
            throw e;
        }

    }

    public MongoDB_S_C_WebSocketMessageEntity itemNameFindRemarkContext(String itemName,StatusType statusType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("itemName").is(itemName).andOperator(Criteria.where("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.ReviewNG)));
        MongoDB_S_C_WebSocketMessageEntity mongoDB_s_c_webSocketSendMessageEntity = mongoTemplate.findOne(query, MongoDB_S_C_WebSocketMessageEntity.class);
        try {
            return mongoDB_s_c_webSocketSendMessageEntity;
        }catch (IndexOutOfBoundsException e) {
            e.printStackTrace();
            throw e;
        }

    }

    public List<MongoDB_S_C_WebSocketSendMessageEntity> findNormalMessages(String uname) {
        Query query = new Query();
        query.addCriteria(Criteria.where("lastUname").is(uname).orOperator(Criteria.where("messageType").is(MessageType.S_CAddAuditMessage)));
        return mongoTemplate.find(query, MongoDB_S_C_WebSocketSendMessageEntity.class);
    }

    public List<MongoDB_S_C_WebSocketSendMessageEntity> findSysMessages(List<User_RoleEntity> user_roleEntities){
        Query query = new Query();
        Criteria criteria = new Criteria();
        List<Criteria> criteriaList = new ArrayList<>();
        for (User_RoleEntity user_roleEntity : user_roleEntities) {
            criteriaList.add(Criteria.where("uRole_tbEntity.uroleId").is(user_roleEntity.getUroleId()).and("messageType").is(MessageType.SysMessage));
        }
        criteria.orOperator(criteriaList.toArray(new Criteria[0]));
        query.addCriteria(criteria);
        return mongoTemplate.find(query,MongoDB_S_C_WebSocketSendMessageEntity.class);
    }

    public boolean deleteMessage(String itemName,String uname) {
        Query mongoDB_S_C_WebSocketMessageEntityQuery = new Query();
        mongoDB_S_C_WebSocketMessageEntityQuery.addCriteria(Criteria.where("itemName").is(itemName));
        Query mongoDB_S_C_Point_ContentEntityQuery = new Query();
        mongoDB_S_C_Point_ContentEntityQuery.addCriteria(Criteria.where("ItemName").is(itemName));
        Query mongoDB_S_C_WebSocketSendMessageEntityQuery = new Query();
        mongoDB_S_C_WebSocketSendMessageEntityQuery.addCriteria(Criteria.where("lastUname").is(uname).and("ItemName").is(itemName));
        boolean flag;
        try {
            mongoTemplate.remove(mongoDB_S_C_WebSocketMessageEntityQuery,MongoDB_S_C_WebSocketMessageEntity.class);
            mongoTemplate.remove(mongoDB_S_C_Point_ContentEntityQuery,MongoDB_S_C_Point_ContentEntity.class);
            mongoTemplate.remove(mongoDB_S_C_WebSocketSendMessageEntityQuery,MongoDB_S_C_WebSocketSendMessageEntity.class);
            flag = true;
        }catch (Exception e){
            e.printStackTrace();
            flag = false;
        }
        return flag;

    }

    public boolean S_C_AuditPass(String itemName,String uname) {

        Query mongoDB_S_C_WebSocketMessageEntityQuery = new Query();
        mongoDB_S_C_WebSocketMessageEntityQuery.addCriteria(Criteria.where("itemName").is(itemName));
        Query mongoDB_S_C_Point_ContentEntityQuery = new Query();
        mongoDB_S_C_Point_ContentEntityQuery.addCriteria(Criteria.where("ItemName").is(itemName).and("Status_Type").is(StatusType.Submitted));


        MongoDB_S_C_WebSocketMessageEntity mongoDB_s_c_webSocketMessageEntity = mongoTemplate.findOne(mongoDB_S_C_WebSocketMessageEntityQuery, MongoDB_S_C_WebSocketMessageEntity.class);
        mongoTemplate.remove(mongoDB_S_C_WebSocketMessageEntityQuery,MongoDB_S_C_WebSocketMessageEntity.class);
        if (mongoDB_s_c_webSocketMessageEntity != null) {
            List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities = mongoTemplate.find(mongoDB_S_C_Point_ContentEntityQuery, MongoDB_S_C_Point_ContentEntity.class);
            mongoTemplate.remove(mongoDB_S_C_Point_ContentEntityQuery,MongoDB_S_C_Point_ContentEntity.class);
            if (mongoDB_s_c_point_contentEntities.size() > 0) {
                List<MongoDB_S_C_Point_ContentEntity> mongoDB_s_c_point_contentEntities1 = new ArrayList<>();
                for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : mongoDB_s_c_point_contentEntities) {

                    mongoDB_s_c_point_contentEntity.setStatus_Type(StatusType.ReviewPassed);
                    mongoDB_s_c_point_contentEntity.setS_C_Type(S_CType.LASTING);
                    mongoDB_s_c_point_contentEntities1.add(mongoDB_s_c_point_contentEntity);

                }
                mongoDB_s_c_webSocketMessageEntity.setMongoDB_s_c_point_contentEntityS(mongoDB_s_c_point_contentEntities1);
                mongoDB_s_c_webSocketMessageEntity.setMessageType(MessageType.NormalMessages);
                for (MongoDB_S_C_Point_ContentEntity mongoDB_s_c_point_contentEntity : mongoDB_s_c_point_contentEntities1) {
                    mongoTemplate.save(mongoDB_s_c_point_contentEntity);
                }
                mongoTemplate.save(mongoDB_s_c_webSocketMessageEntity);


            } else {
                return false;
            }
        }else {
            return false;
        }
        MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = setMessageDate();
        mongoDB_s_c_webSocketSendMessageEntity.setMessage('"' + itemName + '"' + "审核通过，已进入持久存储！");
        mongoDB_s_c_webSocketSendMessageEntity.setItemName(itemName);
        mongoDB_s_c_webSocketSendMessageEntity.setLastUname(uname);
        mongoDB_s_c_webSocketSendMessageEntity.setMessageType(MessageType.S_CAddAuditMessage);

        MongoDB_S_C_WebSocketSendMessageEntity save = mongoTemplate.save(mongoDB_s_c_webSocketSendMessageEntity);
        return save.getLastUname() != null;

    }

    public MongoDB_S_C_WebSocketMessageEntity findS_C_Pass(String itemName,String uname) {

        Query query = new Query();
        query.addCriteria(Criteria.where("itemName").is(itemName)
                .and("lastUserEntity.uname").is(uname)
                .and("mongoDB_s_c_point_contentEntityS.Status_Type").is(StatusType.ReviewPassed.toString())
                .and("mongoDB_s_c_point_contentEntityS.S_C_Type").is(S_CType.LASTING.toString()));
        System.out.println(query);
        MongoDB_S_C_WebSocketMessageEntity one = mongoTemplate.findOne(query, MongoDB_S_C_WebSocketMessageEntity.class);
        System.out.println(one);
        return one;
    }

    public boolean saveSysMessage(MongoDB_S_C_WebSocketMessageEntity messageEntity) {
        MongoDB_S_C_WebSocketSendMessageEntity mongoDB_s_c_webSocketSendMessageEntity = new MongoDB_S_C_WebSocketSendMessageEntity();

        mongoDB_s_c_webSocketSendMessageEntity.setImportantNewsMessageEntity(messageEntity.isImportantNewsMessageEntityIs());
        mongoDB_s_c_webSocketSendMessageEntity.setYear(messageEntity.getYear());
        mongoDB_s_c_webSocketSendMessageEntity.setMonth(messageEntity.getMonth());
        mongoDB_s_c_webSocketSendMessageEntity.setDay(messageEntity.getDay());
        mongoDB_s_c_webSocketSendMessageEntity.setHour(messageEntity.getHour());
        mongoDB_s_c_webSocketSendMessageEntity.setMinute(messageEntity.getMinute());
        mongoDB_s_c_webSocketSendMessageEntity.setSecond(messageEntity.getSecond());
        mongoDB_s_c_webSocketSendMessageEntity.setMessage(messageEntity.getMessage());
        mongoDB_s_c_webSocketSendMessageEntity.setURole_tbEntity(messageEntity.getURole_tbEntity());
        /*mongoDB_s_c_webSocketSendMessageEntity.setMessageCount(messageEntity.getMessageCount());*/
        mongoDB_s_c_webSocketSendMessageEntity.setLastUname(mongoDB_s_c_webSocketSendMessageEntity.getLastUname());
        mongoDB_s_c_webSocketSendMessageEntity.setImportantNewsMessageEntity(messageEntity.isImportantNewsMessageEntityIs());
        mongoDB_s_c_webSocketSendMessageEntity.setMessageType(messageEntity.getMessageType());

        MongoDB_S_C_WebSocketSendMessageEntity save = mongoTemplate.save(mongoDB_s_c_webSocketSendMessageEntity);
        MongoDB_S_C_WebSocketMessageEntity save1 = mongoTemplate.save(messageEntity);
        try {
            return save1.getURole_tbEntity().size() > 0 && save.getURole_tbEntity().size() > 0;
        }catch (NullPointerException nullPointerException){
            nullPointerException.printStackTrace();
            return false;
        }

    }

    public boolean deleteSysMessage(String message, Integer date, Integer month, Integer hour, MessageType messageType) {
        Query query = new Query();
        query.addCriteria(Criteria.where("message").is(message).and("messageType").is(messageType).and("month").is(month).and("day").is(date).and("hour").is(hour));



        DeleteResult remove = mongoTemplate.remove(query, MongoDB_S_C_WebSocketMessageEntity.class);
        DeleteResult remove1 = mongoTemplate.remove(query, MongoDB_S_C_WebSocketSendMessageEntity.class);

        return remove.getDeletedCount() > 0 || remove1.getDeletedCount() > 0;
    }

    public boolean SysAuthentication(MongoDB_S_C_SysAuthAuditMessageEntity messageEntity){
        Query query = new Query();
        query.addCriteria(Criteria.where("existUserEntity.uname").is(messageEntity.getExistUserEntity().getUname()));
        List<MongoDB_S_C_SysAuthAuditMessageEntity> mongoDB_s_c_saveSCDailyContextEntities = mongoTemplate.find(query, MongoDB_S_C_SysAuthAuditMessageEntity.class);
        if (mongoDB_s_c_saveSCDailyContextEntities.isEmpty()){
            MongoDB_S_C_SysAuthAuditMessageEntity save = mongoTemplate.save(messageEntity);
            return save.getExistUserEntity().getUname() != null;
        }else {
            return false;
        }

    }

    public List<MongoDB_S_C_SysAuthAuditMessageEntity> findUserAuthAuditMessage(Group_Type group_type){
        Query query = new Query();
        query.addCriteria(Criteria.where("AuditGroup").is(group_type));
        return mongoTemplate.find(query,MongoDB_S_C_SysAuthAuditMessageEntity.class);
    }





}
